<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - 2ch</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-2ch.html">here</a>.</p>


<div id="2ch" style="display: none;">
<b>${query}</b>で2chのスレッドタイトルを検索:
<br/><br/>
{for item in items}
  <div style="clear: both;font-size:90%;">
    <b>${item.key} <a href="${item.url}" accesskey="${item.key}">${item.title}</a></b>
    <br/>
    <div style="margin-left: 20px; font-size: 90%;">
      ${item.description}
    </div>
  </div>
{forelse}
  <b>該当項目が見つかりませんでした</b>
{/for}
<div style="font-size:70%;clear:both;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
</div>


<script class="commands"><![CDATA[

CmdUtils.CreateCommand({
    name: "2ch",
    description: "Find.2ch でスレッドタイトルを検索します",
    takes: {search: noun_arb_text},
    icon: "http://find.2ch.net/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "2ch のスレッドをタイトルで検索";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を 2ch のスレッドタイトルから検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        var searchUrl = get2chUrl(searchText);
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: searchUrl,
          dataType: "html",
          
          beforeSend: function(xhr){
            xhr.overrideMimeType("text/html;charset=EUC-JP");
          },
            
          error: function() {
            previewBlock.innerHTML = "Error searching Find.2ch.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            var wrapper = responseData.find(".content_pane dl");
            
            var items = [];
            var count = wrapper.find("dt").length;
            
            count = ( count>=5 ) ? 5 : count;
            
            for (var i = 0; i < count; i++){
                
                
                wrapper.find(".r_sec_body:eq(" + i + ") a").each(function(){
                    var anchor = jQuery(this);
                    var anchorVal = anchor.attr("href");
                    
                    if(parseUri(anchorVal).host == ""){
                        anchor.attr("href", "http://find.2ch.net/" + anchorVal );
                    }
                    
                });
                
                /*
                wrapper.find(".r_sec_body:eq(" + i + ") a").attr("href", function(){
                    return addBaseUrl(this, "http://find.2ch.net/");
                });
                */
                var item = {
                    title : wrapper.find("dt:eq(" + i + ")").html(),
                    description : wrapper.find(".r_sec_body:eq(" + i + ")").html(),
                    key: i+1,
                    url: wrapper.find("dt:eq(" + i + ") a:first").attr("href")
                };
                
                items.push(item);
                
            }
            
            var previewData = {
                query: searchText,
                items: items
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#2ch", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( get2chUrl(directObject.text) );
    }
});

function get2chUrl(query){
    
    query = Utils.convertFromUnicode("Shift-JIS", query);
    query = escape(query);
    
    var url = "http://find.2ch.net/?BBS=ALL&TYPE=TITLE&IE=SJIS&OS&STR={QUERY}";
    url = url.replace(/%s|{QUERY}/g, query);
    return url;
}


function addBaseUrl(href, baseUrl){
    
    uri = parseUri(href);
    
    if(uri.host == ""){
        href = baseUrl + href;
    }else if(uri.host == "ubiquity"){
        href = baseUrl + uri.relative;
    }
    
    return href;
}

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: true,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


]]></script>
</body>
</html>
